package com.me.jingdongshop.ui.fragment;

import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ListView;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.me.common.base.BaseFragment;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.Home;
import com.me.jingdongshop.model.bean.HomeRecommendedBrand;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.DiscoverFragmentPresenter;
import com.me.jingdongshop.ui.adapter.DisAdapterLv;

import java.util.List;

/**
 * Created by YL on 2017/5/4.
 */
public class DiscoverFragment extends BaseFragment{

    private ListView disLv;
    private SliderLayout mSliderLayout;
    private PagerIndicator indicator;
    @Override
    public int getLayoutRes() {
        return R.layout.fragment_discover;
    }

    @Override
    public void initView() {

        mSliderLayout = (SliderLayout) mRoot.findViewById(R.id.slider);
        disLv = (ListView) mRoot.findViewById(R.id.dis_lv);
        indicator =(PagerIndicator) mRoot.findViewById(R.id.dis_indicator);

    }

    @Override
    public void initListener() {


    }

    @Override
    public void initData() {

        DiscoverFragmentPresenter mPresenter =
                new DiscoverFragmentPresenter(this);
        //列表
        mPresenter.getBrandData();
        //轮播图
        mPresenter.getBannerDatats();


    }

    @Override
    public void onClick(View v, int id) {

    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        if (reqType== IHttpService.HTTP_BRAND){

            HomeRecommendedBrand brand =
                    (HomeRecommendedBrand) msg.obj;
            List<HomeRecommendedBrand.BrandBean> brand1 = brand.getBrand();

           // 拿到列表的每个item的所有数据

            disLv.setAdapter(new DisAdapterLv(getContext(),brand1));

        }else if (reqType==IHttpService.HTTP_HOME){

            Home home = (Home) msg.obj;
            List<Home.HomeTopicBean> homeTopic = home.getHomeTopic();


            mSliderLayout.removeAllSliders();

            for (int i = 0; i <homeTopic.size(); i++) {
                TextSliderView textSliderView = new TextSliderView(getActivity());
                String url = IHttpService.HOST_URL+homeTopic.get(i).getPic();
                textSliderView.image(url);
                textSliderView.error(R.drawable.ic_launcher);
                textSliderView.empty(R.drawable.ic_launcher);
                mSliderLayout.addSlider(textSliderView);

            }
            //自定义指示器
            mSliderLayout.setCustomIndicator(indicator);

            //设置viewpager的翻页效果
            mSliderLayout.setPresetTransformer(SliderLayout.Transformer.Default);
            //设置翻页时间
            mSliderLayout.setDuration(2000);
            //设置指示器背景动画
            mSliderLayout.setCustomAnimation(new DescriptionAnimation());


        }



    }

    @Override
    public void onHttpError(int reqType, String error) {

    }
}
